package com.mybed.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybed.crm.anno.Log;
import com.mybed.crm.pojo.dto.PageActivityDto;
import com.mybed.crm.pojo.pojo.Activity;
import com.mybed.crm.pojo.vo.ActivityVo;
import com.mybed.crm.result.PageResult;
import com.mybed.crm.result.Result;
import com.mybed.crm.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/clues/activity")
@Slf4j
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 分页查询活动信息
     */
    @GetMapping("/list")
    public PageResult PageActivityQuery(PageActivityDto dto) {
        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper();
        wrapper.like(StringUtils.hasLength(dto.getName()), Activity::getName, dto.getName());
        wrapper.eq(StringUtils.hasLength(dto.getChannel()), Activity::getChannel, dto.getChannel());
        wrapper.eq(StringUtils.hasLength(dto.getCode()), Activity::getCode, dto.getCode());
        if (dto.getParams() != null) {
            wrapper.between(StringUtils.hasLength(dto.getParams().get("beginCreateTime"))
                            && StringUtils.hasLength(dto.getParams().get("endCreateTime"))
                    , Activity::getCreateTime, dto.getParams().get("beginCreateTime")
                    , dto.getParams().get("endCreateTime"));
        }
        IPage<Activity> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        IPage<Activity> iPage = activityService.page(page, wrapper);
        PageResult result = new PageResult();
        result.insertData(iPage);
        return result;
    }

    /**
     * 新增活动信息
     */
    @Log("新增活动信息")
    @PostMapping
    public Result addActivity(@RequestBody Activity activity) {
        String uniqueCode = getShortUUID(5);
        activity.setCode(uniqueCode);
        log.info("新增活动信息：{}", activity);
        return Result.success(activityService.add(activity));
    }

    /**
     * 根据活动id查询活动信息
     */
    @GetMapping("/{id}")
    public Result<ActivityVo> selectActivityById(@PathVariable("id") Long id) {
        log.info("根据活动id查询活动信息：{}", id);
        Activity activity = activityService.getById(id);
        ActivityVo activityVo = new ActivityVo();
        BeanUtils.copyProperties(activity, activityVo);
        return Result.success(activityVo);
    }

    /**
     * 删除活动信息
     */
    @Log("删除活动信息")
    @DeleteMapping("/{id}")
    public Result deleteActivity(@PathVariable("id") Long id) {
        log.info("删除活动信息：{}", id);
        activityService.removeById(id);
        return Result.success();
    }

    /**
     * 修改活动信息
     */
    @Log("修改活动信息")
    @PutMapping
    public Result updateActivity(@RequestBody Activity activity) {
        log.info("修改活动信息：{}", activity);
        activityService.updateById(activity);
        return Result.success();
    }

    /**
     * 根据Id查询活动详情
     *
     * @param id
     * @return
     */
    @GetMapping("/listselect/{id}")
    public Result<List<Activity>> selectActivitysById(@PathVariable Long id) {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("channel", id);
        List<Activity> list = activityService.list(queryWrapper);
        return Result.success(list);
    }

    // 生成短UUID
    private String getShortUUID(int length) {
        UUID uuid = UUID.randomUUID();
        String uuidStr = uuid.toString().replace("-", "");
        return uuidStr.substring(0, Math.min(length, uuidStr.length()));
    }
}
